<?php

/**
* Controller:  "Cancel"
*
* This controller allows a logged-in user to cancel an event from the database.
* An event selected froma drop-down list is removed from the database, and can
* no longer be viewed from the public "Events" page.
*
* Functions:  index
*
* @author     Brandon A Johnson
* @copyright  LIS4368-01: "Web Development with PHP," Spring 2010
* @version    1.0
* @date       April 24, 2010
*
*/

class Cancel extends Controller {
	
	/**
	* Cancel() - constructor
	*
	* allows this file to act as a valid CodeIgniter controller
	*/
	function Cancel()
	{
		parent::Controller();
	}

	/**
	* index() - default function
	*
	* takes the event the user selected from the secure section and removes it
	* from the database, effectively "cancelling" the event
	*/
	function index()
	{
		// loads the "events" database interaction model
		$this->load->model('events_model');
		// extracts all event records currently in the table
		$all_events = $this->events_model->list_events();
		// prepares the data to be sent to the view
		$data = array();
		$data['events'] = $all_events;

		// if no event was selected, print an error message and ask the user to try again
		if($_POST['events'] == NULL)
		{
			echo "No event was selected.  Please try again.";

			$this->load->view('header');
			$this->load->view('secure', $data);
		}
		// otherwise...
		else
		{
			// prepare the POST data to be sent to the database
			$event_to_cancel = $_POST['events'];

			// remove the event from the database
			$this->events_model->cancel_event($event_to_cancel);
		}

		// gets all the event records still present and prepares them for the view
		$all_events = $this->events_model->list_events();
		$data = array();
		$data['events'] = $all_events;
		
		// loads the site header and the secure section, complete with the updated events record
		$this->load->view('header');
		$this->load->view('cancel_event', $data);
	}
}

/* End of file cancel.php */
/* Location: ./system/application/controllers/cancel.php */